package com.blockmeta.bbs.businesslibrary.baselist;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.net.okhttp.AdapterLoadDataBusiness;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public abstract class NewBaseListActivity<H, A extends BaseBean, B extends BaseListModelImpl<A>> extends BaseActivity implements GetModelImpl<A,B>, OnLoadmoreListener, OnRefreshListener {
    public RxAppCompatActivity mContext;
    private View view;
    public SmartRefreshLayout list_swipe;
    public RecyclerView list_recy;
    private FrameLayout header_container;
    private FrameLayout header_containerout;
    public LinearLayout mContainer;

    protected ArrayList<H> mList = new ArrayList<>();
    public BaseQuickAdapter<H, BaseViewHolder> mAdapter;

    @Override
    public View setView() {
        mContext = NewBaseListActivity.this;
        initBundle();
        view = LayoutInflater.from(mContext).inflate(R.layout.list_smart_header_new, null);
        list_swipe = (SmartRefreshLayout) view.findViewById(R.id.list_swipe);
        list_recy = (RecyclerView) view.findViewById(R.id.list_recy);
        header_container = (FrameLayout) view.findViewById(R.id.header_framelayout);
        header_containerout = (FrameLayout) view.findViewById(R.id.header_framelayoutout);
        mContainer = (LinearLayout) view.findViewById(R.id.ll_container);
        list_recy.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        list_swipe.setOnLoadmoreListener(this);
        list_swipe.setOnRefreshListener(this);
        list_swipe.setRefreshFooter(new ClassicsFooter(mContext));
        if (addNonScrollHeaderView() == null) {
            header_container.setVisibility(View.GONE);
        } else {
            header_container.addView(addNonScrollHeaderView());
        }
        if (addOutHeaderView() == null) {
            header_containerout.setVisibility(View.GONE);
        } else {
            header_containerout.setVisibility(View.VISIBLE);
            header_containerout.addView(addOutHeaderView());
        }
        ((SimpleItemAnimator)list_recy.getItemAnimator()).setSupportsChangeAnimations(false);
        if(mAdapter == null){
            mAdapter = new BaseListAdapter<H>(mList, getItemLayout(), mContext) {
                @Override
                protected void bindView(BaseViewHolder helper, H item) {
                    fillData(helper, item);
                }
            };
            View empty = LayoutInflater.from(mContext).inflate(R.layout.empty_data, null);
            mAdapter.setEmptyView(empty);
            addHeaderData(mAdapter);
            list_recy.setAdapter(mAdapter);
            list_swipe.autoRefresh(200);
        }
        doSomeBindEvent();

        return view;
    }

    protected void doSomeBindEvent() {

    }

    protected abstract void fillData(BaseViewHolder helper, H item);

    @Override
    public void onLoadmore(RefreshLayout refreshlayout){
        getMyModelImpl().getMoreData(mContext);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout){
        getMyModelImpl().getRefreshDta(mContext);
        getMyModelImpl().loadComplete(new BaseListModel.LoadComplete() {

            @Override
            public void getDataRefresh(List<?> mDatas) {
                AdapterLoadDataBusiness.finishLoadAndRefresh(list_swipe);
                mAdapter.setNewData((List<H>) mDatas);
//                list_swipe.finishRefresh(true);
            }

            @Override
            public void getDataMore(List<?> mDatas) {
                AdapterLoadDataBusiness.finishLoadAndRefresh(list_swipe);
                if (mDatas == null || mDatas.size() == 0) {
                    list_swipe.finishLoadmore();
                    return;
                }
                mAdapter.addData((Collection<? extends H>) mDatas);

            }

            @Override
            public void getDataFailed(BaseBean baseBean) {

            }

            @Override
            public void hasNoMoreData() {
                AdapterLoadDataBusiness.finishLoadAndRefresh(list_swipe);
                list_swipe.finishLoadmoreWithNoMoreData();
            }
        });
    }



    protected abstract int getItemLayout();

    protected abstract View addNonScrollHeaderView();

    protected abstract View addOutHeaderView();

    protected abstract void initBundle();


    /**
     * 添加头部
     * 满足业务拓展需要
     */
    public void addHeaderData(BaseQuickAdapter mAdapter) {

    }

    /**
     * 处理页面中的点击事件
     *
     * @param adapter
     * @param view
     * @param position
     */
    public void setAdapterClick(BaseQuickAdapter adapter, View view, int position) {

    }


}
